package gjbsh.bangong.ds.controller;

import java.math.BigDecimal;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import gjbsh.bangong.ds.model.组织活动单位参会名额;
import gjbsh.bangong.ds.service.组织活动单位参会名额Service;
import gjbsh.support.JsonResult;



@RestController
@RequestMapping("zuzhihuodongbdanweicanhuiminge")
public class 组织活动单位参会名额Controller {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	组织活动单位参会名额Service dd组织活动单位参会名额Service;


	// 查询单位名额
	@RequestMapping("find_by_deptname_and_huodongid")
	public JsonResult<?> find_By_DeptName_And_HuodongId(@RequestParam("huodongid")String ppHuodongId,@RequestParam("deptname")String ppDeptName){
		try {
			if("".equals(ppHuodongId)) {
				return JsonResult.getErrorResult("组织活动ID不能为空");
			}
			return JsonResult.getSuccessResult(dd组织活动单位参会名额Service.findByDeptNameAndHuodongId(ppHuodongId, ppDeptName));
		} catch (Exception e) {
			
			logger.error("组织活动单位参会名额Controller->find_by_deptname_and_huodongid: "+e.getMessage());
			return JsonResult.getErrorResult("查询单位名额失败");
		}
	}

	// 查询单位名额
	@RequestMapping("find_by_huodongid")
	public JsonResult<?> findByHuodongId(@RequestParam("huodongid")String ppHuodongId){
		try {
			if("".equals(ppHuodongId)) {
				return JsonResult.getErrorResult("组织活动ID不能为空");
			}
			return JsonResult.getSuccessResult(dd组织活动单位参会名额Service.findByHuodongId(ppHuodongId));
		} catch (Exception e) {
			
			logger.error("组织活动单位参会名额Controller->find_by_huodongid: "+e.getMessage());
			return JsonResult.getErrorResult("查询单位名额失败");
		}
	}
	
	// 通过主键查询单位名额
	@RequestMapping("find_by_mingeid")
	public JsonResult<?> findByMingeId(@RequestParam("mingeid")String ppMingeId){
		try {
			if("".equals(ppMingeId)) {
				return JsonResult.getErrorResult("名额ID不能为空");
			}
			return JsonResult.getSuccessResult(dd组织活动单位参会名额Service.findOne(ppMingeId));
		} catch (Exception e) {
			logger.error("组织活动单位参会名额Controller->find_by_mingeid: "+e.getMessage());
			return JsonResult.getErrorResult("查询指定名额失败");
		}
	}

	// 添加
	@RequestMapping("add")
	public JsonResult<?> Add(@RequestParam("huodongid")String ppHuodongId,@RequestParam("deptid")String ppDeptId,@RequestParam("num")int ppNum){
		try {
			if("".equals(ppDeptId)) {
				return JsonResult.getErrorResult("单位ID不能为空");
			}
			if("".equals(ppHuodongId)) {
				return JsonResult.getErrorResult("活动ID不能为空");
			}
			
			组织活动单位参会名额 mm组织活动单位参会名额 = new 组织活动单位参会名额();
			mm组织活动单位参会名额.set参会人数(new BigDecimal(ppNum));
			mm组织活动单位参会名额.set组织活动id(ppHuodongId);
			mm组织活动单位参会名额.set组织活动单位参会名额id(UUID.randomUUID().toString());
			mm组织活动单位参会名额.set设站单位id(ppDeptId);
			
			boolean isok = dd组织活动单位参会名额Service.addAll(mm组织活动单位参会名额);
			if(isok) {
				return JsonResult.getSuccessResult("添加成功");
			}else {
				return JsonResult.getErrorResult("添加失败");
			}
		} catch (Exception e) {
			logger.error("组织活动单位参会名额Controller->add: "+e.getMessage());
			return JsonResult.getErrorResult("添加单位名额失败");
		}
	}

	@RequestMapping("delete")
	public JsonResult<?> Delete(@RequestParam("mingeid")String ppMingeId){
		try {
			
			if("".equals(ppMingeId)) {
				return JsonResult.getErrorResult("名额ID不能为空");
			}
			
			boolean isok = dd组织活动单位参会名额Service.delete(ppMingeId);
			if(isok) {
				return JsonResult.getSuccessResult("删除成功");
			}else {
				return JsonResult.getErrorResult("删除失败");
			}
			
			
			
		} catch (Exception e) {
			logger.error("组织活动单位参会名额Controller->delete: "+e.getMessage());
			return JsonResult.getErrorResult("删除单位名额失败");
		}
	}
	
	@RequestMapping("modify")
	public JsonResult<?> Modify(@RequestParam("mingeid")String ppMingeId,@RequestParam("huodongid")String ppHuodongId,@RequestParam("deptid")String ppDeptId,@RequestParam("num")int ppNum){
		try {
			
			if("".equals(ppMingeId)) {
				return JsonResult.getErrorResult("名额ID不能为空");
			}
			组织活动单位参会名额 mm组织活动单位参会名额 = dd组织活动单位参会名额Service.findOne(ppMingeId);
			if(mm组织活动单位参会名额 == null) {
				return  JsonResult.getErrorResult("该组织活动不存在");
			}
			mm组织活动单位参会名额.set参会人数(new BigDecimal(ppNum));
			mm组织活动单位参会名额.set组织活动id(ppHuodongId);
			mm组织活动单位参会名额.set设站单位id(ppDeptId);
			
			boolean isok = dd组织活动单位参会名额Service.Modify(mm组织活动单位参会名额);
			if(isok) {
				return JsonResult.getSuccessResult("修改成功");
			}else {
				return JsonResult.getErrorResult("修改失败");
			}
			
		} catch (Exception e) {
			logger.error("组织活动单位参会名额Controller->modify: "+e.getMessage());
			return JsonResult.getErrorResult("修改单位名额失败");
		}
	}

}
